/*
 * CoordTransform.java
 *
 * Created on September 2, 2007, 1:07 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.util;

import com.jhlabs.map.proj.Projection;
import com.jhlabs.map.proj.ProjectionFactory;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 *
 * @author demory
 */
public class CoordTransform {

  /** Creates a new instance of CoordTransform */
  /*public CoordTransform() {
  }*/
  public static Point2D.Double fromLonLat(Point2D.Double ll) {
    double x = ll.getX(), y = ll.getY();
    //System.out.println("coordtransform start");

    // State Plane GA-West 
    Projection proj = ProjectionFactory.fromPROJ4Specification(
            new String[]{
      "+proj=tmerc",
      "+datum=NAD83",
      "+lat_0=30",
      "+lon_0=-84.166667",
      "+k=0.9999",
      "+x_0=699998.6"
    });
    Point2D.Double src = ll, dst = new Point2D.Double();

    proj.transform(src, dst);
    //System.out.println(dst.toString());
    //System.out.println("coordtransform done");
    return dst;
  }

  public static Point2D.Double toLonLat(Point2D.Double sp) {
    double x = sp.getX(), y = sp.getY();
    //System.out.println("coordtransform start");
    Projection proj = ProjectionFactory.fromPROJ4Specification(
            new String[]{
      "+proj=tmerc",
      "+datum=NAD83",
      "+lat_0=30",
      "+lon_0=-84.166667",
      "+k=0.9999",
      "+x_0=699998.6"
    });
    Point2D.Double src = sp, dst = new Point2D.Double();

    proj.inverseTransform(src, dst);
    //System.out.println(dst.toString());
    //System.out.println("coordtransform done");
    return dst;
  }

  public static final double M_TO_FT = 3.2808399;
  
  public static Point2D.Double feetToMeters(Point2D.Double pt) {
    return new Point2D.Double(pt.getX()/M_TO_FT, pt.getY()/M_TO_FT);
  }
  
  
  // not used:
  public static Point2D fromLatLon2(Point2D ll) {
    double x = ll.getX(), y = ll.getY();
    System.out.println("coordtransform start");
    try {
      for (int i = 0; i < 100; i++) {
        String coords = ll.getX() + " " + ll.getY();

        //String cmd = "c:\\demory\\apps\\proj\\bin\\cs2cs.exe -I +proj=tmerc +datum=NAD83 +lat_0=30 +lon_0=-84.166667 +k=0.9999 +x_0=699998.6 +to +proj=latlong +datum=WGS84";
        Process p = Runtime.getRuntime().exec("cmd /c echo " + coords + " | c:\\demory\\apps\\proj\\bin\\cs2cs.exe -I +proj=tmerc +datum=NAD83 +lat_0=30 +lon_0=-84.166667 +k=0.9999 +x_0=699998.6 +to +proj=latlong +datum=WGS84");
        //ProcessBuilder pb = new ProcessBuilder(cmd);//, args);//
        //pb.directory(new File("c:\\demory\\apps\\proj\\bin"));
        //Process p = pb.start();

        /*StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
        StreamGobbler errGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
        outGobbler.start();
        errGobbler.start();*/

        //BufferedOutputStream out = new BufferedOutputStream(p.getOutputStream());

        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String s = br.readLine();
        p.destroy();
        br.close();
        System.out.println(s);
      }

    } catch (Exception ex) {
      ex.printStackTrace();
    }
    System.out.println("coordtransform done");
    return new Point2D.Double(0, 0);
  }

  public static class ProcessReader extends Thread {

    private BufferedReader br_;
    private boolean ready_,  readInput_;

    public ProcessReader(InputStream is) {
      br_ = new BufferedReader(new InputStreamReader(is));
      ready_ = readInput_ = false;
    }

    public void run() {
      try {
        System.out.println("waiting...");
        String result = br_.readLine();
        ready_ = true;
        System.out.println("result: " + result);
        readInput_ = true;
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }

    public boolean isReady() {
      return ready_;
    }

    public boolean readInput() {
      return readInput_;
    }
  }

}
